package com.data.modules.online.cgreport.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.data.common.util.DateUtils;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.OnlFormShowType;
import com.data.modules.online.cgreport.common.CgReportConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CgReportQueryParamUtil {

    private static final Logger f394a = LoggerFactory.getLogger(CgReportQueryParamUtil.class);

    public static void assembleMap(HttpServletRequest httpServletRequest, Map<String, Object> map, Map<String, Object> map2, Map<String, Object> map3) {
        String str;
        String str2 = (String) map.get(CgReportConstant.f368o);
        String str3 = (String) map.get(CgReportConstant.f371r);
        String str4 = (String) map.get(CgReportConstant.f370q);
        if (CgformUtil.f232F.equals(str3)) {
            String parameter = httpServletRequest.getParameter(str2.toLowerCase());
            try {
                if (!ConvertUtils.isEmpty(parameter)) {
                    if (httpServletRequest.getQueryString().contains(str2 + CgformUtil.f302m)) {
                        str = new String(parameter.getBytes("ISO-8859-1"), "UTF-8");
                    } else {
                        str = parameter;
                    }
                    if (ConvertUtils.isNotEmpty(str)) {
                        if (str.contains("*")) {
                            str = str.replaceAll("\\*", "%");
                            map2.put(str2, " LIKE :" + str2);
                        } else {
                            map2.put(str2, " = :" + str2);
                        }
                    }
                    map3.put(str2, assembleDate(str4, str, true));
                }
            } catch (UnsupportedEncodingException e) {
                f394a.error(e.getMessage(), e);
            }
        } else if ("group".equals(str3)) {
            String parameter2 = httpServletRequest.getParameter(str2.toLowerCase() + "_begin");
            String parameter3 = httpServletRequest.getParameter(str2.toLowerCase() + "_end");
            if (ConvertUtils.isNotEmpty(parameter2)) {
                map2.put(str2, " >= :" + str2 + "_begin");
                map3.put(str2 + "_begin", assembleDate(str4, parameter2, true));
            }
            if (ConvertUtils.isNotEmpty(parameter3)) {
                map2.put(new String(str2), " <= :" + str2 + "_end");
                map3.put(str2 + "_end", assembleDate(str4, parameter3, false));
            }
        }
    }

    private static Object assembleDate(String str, String str2, boolean z) {
        if (!ConvertUtils.isNotEmpty(str2)) {
            return null;
        }
        if (CgReportConstant.f379z.equalsIgnoreCase(str)) {
            return str2;
        }
        if ("Date".equalsIgnoreCase(str)) {
            if (str2.length() != 19 && str2.length() == 10) {
                if (z) {
                    str2 = str2 + " 00:00:00";
                } else {
                    str2 = str2 + " 23:59:59";
                }
            }
            return DateUtils.str2Date(str2, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        }
        if (CgReportConstant.f348B.equalsIgnoreCase(str) || !"Integer".equalsIgnoreCase(str)) {
        }
        return str2;
    }

    public static String m356a(List<Map<String, Object>> list, Long l) {
        String str;
        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArray = new JSONArray();
        jSONObject.put("total", l);
        if (list != null) {
            for (Map<String, Object> map : list) {
                JSONObject jSONObject2 = new JSONObject();
                for (String str2 : map.keySet()) {
                    String valueOf = String.valueOf(map.get(str2));
                    String lowerCase = str2.toLowerCase();
                    if (lowerCase.contains("time") || lowerCase.contains(OnlFormShowType.f320d)) {
                        str = formatDate(valueOf);
                    } else {
                        str = valueOf;
                    }
                    jSONObject2.put(lowerCase, str);
                }
                jSONArray.add(jSONObject2);
            }
        }
        jSONObject.put("rows", jSONArray);
        return jSONObject.toString();
    }

    public static String m355a(List<Map<String, Object>> list) {
        String str;
        JSONArray jSONArray = new JSONArray();
        for (Map<String, Object> map : list) {
            JSONObject jSONObject = new JSONObject();
            for (String str2 : map.keySet()) {
                String valueOf = String.valueOf(map.get(str2));
                String lowerCase = str2.toLowerCase();
                if (lowerCase.contains("time") || lowerCase.contains(OnlFormShowType.f320d)) {
                    str = formatDate(valueOf);
                } else {
                    str = valueOf;
                }
                jSONObject.put(lowerCase, str);
            }
            jSONArray.add(jSONObject);
        }
        return jSONArray.toString();
    }

    public static String formatDate(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(simpleDateFormat.parse(str));
        } catch (Exception e) {
            return str;
        }
    }
}
